/************************************************************************************/
/*  Program name CAPGLIMP_CODE_COMB.sql                                             */
/*  Creater:  NIP Karen (PCCW - UniHub)                                             */
/*************   History   *************************                                */
/* 23 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_CODE_COMB';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_CODE_COMB;
	
	lv_Next_Seq_ID		NUMBER;

BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	GL_CODE_COMBINATIONS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT	INTO CRST_GL_CODE_COMB_MAP	VALUES
			(
			C1.CODE_COMBINATION_ID,
			lv_Next_Seq_ID
			);
			
			INSERT INTO GL_CODE_COMBINATIONS
			VALUES
			(
			lv_Next_Seq_ID,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetCOAIDfromFlexStrCode(C1.ID_FLEX_STR_CODE),
			--C1.CHART_OF_ACCOUNTS_ID,
			C1.DETAIL_POSTING_ALLOWED_FLAG,
			C1.DETAIL_BUDGETING_ALLOWED_FLAG,
			C1.ACCOUNT_TYPE,
			C1.ENABLED_FLAG,
			C1.SUMMARY_FLAG,
			C1.SEGMENT1,
			C1.SEGMENT2,
			C1.SEGMENT3,
			C1.SEGMENT4,
			C1.SEGMENT5,
			C1.SEGMENT6,
			C1.SEGMENT7,
			C1.SEGMENT8,
			C1.SEGMENT9,
			C1.SEGMENT10,
			C1.SEGMENT11,
			C1.SEGMENT12,
			C1.SEGMENT13,
			C1.SEGMENT14,
			C1.SEGMENT15,
			C1.SEGMENT16,
			C1.SEGMENT17,
			C1.SEGMENT18,
			C1.SEGMENT19,
			C1.SEGMENT20,
			C1.SEGMENT21,
			C1.SEGMENT22,
			C1.SEGMENT23,
			C1.SEGMENT24,
			C1.SEGMENT25,
			C1.SEGMENT26,
			C1.SEGMENT27,
			C1.SEGMENT28,
			C1.SEGMENT29,
			C1.SEGMENT30,
			C1.DESCRIPTION,
			C1.TEMPLATE_ID,
			C1.ALLOCATION_CREATE_FLAG,
			C1.START_DATE_ACTIVE,
			C1.END_DATE_ACTIVE,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.CONTEXT,
			C1.SEGMENT_ATTRIBUTE1,
			C1.SEGMENT_ATTRIBUTE2,
			C1.SEGMENT_ATTRIBUTE3,
			C1.SEGMENT_ATTRIBUTE4,
			C1.SEGMENT_ATTRIBUTE5,
			C1.SEGMENT_ATTRIBUTE6,
			C1.SEGMENT_ATTRIBUTE7,
			C1.SEGMENT_ATTRIBUTE8,
			C1.SEGMENT_ATTRIBUTE9,
			C1.SEGMENT_ATTRIBUTE10,
			C1.SEGMENT_ATTRIBUTE11,
			C1.SEGMENT_ATTRIBUTE12,
			C1.SEGMENT_ATTRIBUTE13,
			C1.SEGMENT_ATTRIBUTE14,
			C1.SEGMENT_ATTRIBUTE15,
			C1.SEGMENT_ATTRIBUTE16,
			C1.SEGMENT_ATTRIBUTE17,
			C1.SEGMENT_ATTRIBUTE18,
			C1.SEGMENT_ATTRIBUTE19,
			C1.SEGMENT_ATTRIBUTE20,
			C1.SEGMENT_ATTRIBUTE21,
			C1.SEGMENT_ATTRIBUTE22,
			C1.SEGMENT_ATTRIBUTE23,
			C1.SEGMENT_ATTRIBUTE24,
			C1.SEGMENT_ATTRIBUTE25,
			C1.SEGMENT_ATTRIBUTE26,
			C1.SEGMENT_ATTRIBUTE27,
			C1.SEGMENT_ATTRIBUTE28,
			C1.SEGMENT_ATTRIBUTE29,
			C1.SEGMENT_ATTRIBUTE30,
			C1.SEGMENT_ATTRIBUTE31,
			C1.SEGMENT_ATTRIBUTE32,
			C1.SEGMENT_ATTRIBUTE33,
			C1.SEGMENT_ATTRIBUTE34,
			C1.SEGMENT_ATTRIBUTE35,
			C1.SEGMENT_ATTRIBUTE36,
			C1.SEGMENT_ATTRIBUTE37,
			C1.SEGMENT_ATTRIBUTE38,
			C1.SEGMENT_ATTRIBUTE39,
			C1.SEGMENT_ATTRIBUTE40,
			C1.SEGMENT_ATTRIBUTE41,
			C1.SEGMENT_ATTRIBUTE42,
			C1.REFERENCE1,
			C1.REFERENCE2,
			C1.REFERENCE3,
			C1.REFERENCE4,
			C1.REFERENCE5,
			C1.JGZZ_RECON_FLAG,
			C1.JGZZ_RECON_CONTEXT,
			C1.PRESERVE_FLAG,
			C1.REFRESH_FLAG,
			C1.IGI_BALANCED_BUDGET_FLAG,
			C1.COMPANY_COST_CENTER_ORG_ID,
			C1.REVALUATION_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/